package org.jeecgframework.web.tcdev.controller;

import org.jboss.logging.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MutiLangUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.tcdev.entity.PlatformConfig;
import org.jeecgframework.web.tcdev.service.PlatformConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 平台配置
 * Created by zjy on 2015/9/22.
 */
@Controller
@RequestMapping("/platformConfigController")
public class PlatformConfigController {

    private static final Logger logger = Logger.getLogger(PlatformConfigController.class);

    @Autowired
    private PlatformConfigService platformConfigService;

    @Autowired
    private SystemService systemService;

    private String message;


    /**
     * 平台配置列表页
     * @return
     */
    @RequestMapping(params = "platformConfigList")
    public ModelAndView platformConfigList(){
        return new ModelAndView("tcdev/platformConfig/platformConfigList");
    }


    /**
     * 平台配置列表
     * @param platformConfig
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(PlatformConfig platformConfig,HttpServletRequest request,HttpServletResponse response,DataGrid dataGrid){


        CriteriaQuery cq = new CriteriaQuery(PlatformConfig.class,dataGrid);

        // 查询条件组装
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, platformConfig, request.getParameterMap());
        this.platformConfigService.getDataGridReturn(cq,true);
        TagUtil.datagrid(response, dataGrid);
    }


    /**
     * 跳转到修改转视图
     * @return
     */
    @RequestMapping(params = "addorupdate")
    public ModelAndView addorupdate(PlatformConfig platformConfig,
                                    HttpServletRequest req) {


        if (platformConfig.getConfigId() != null) {
            platformConfig = platformConfigService.getEntity(PlatformConfig.class, platformConfig.getConfigId());

            req.setAttribute("platformCfg", platformConfig);

        }


        return new ModelAndView("tcdev/platformConfig/platformConfig");
    }



    /**
     * 添加平台配置
     *
     * @param platformConfig
     * @return
     */
    @RequestMapping(params = "save")
    @ResponseBody
    public AjaxJson save(PlatformConfig platformConfig, HttpServletRequest request) {

        AjaxJson j = new AjaxJson();
        if (platformConfig.getConfigId() != null) {

            try {

                PlatformConfig c = platformConfigService.get(PlatformConfig.class, platformConfig.getConfigId());


                MyBeanUtils.copyBeanNotNull2Bean(platformConfig, c);

                platformConfigService.saveOrUpdate(c);

                message = MutiLangUtil.paramUpdSuccess("平台配置["+platformConfig.getPfConfig()+"]");

            } catch (Exception e) {
                e.printStackTrace();
                message = MutiLangUtil.paramUpdFail("平台配置["+platformConfig.getPfConfig()+"]");
            }
            systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        } else {

            try {


                platformConfigService.save(platformConfig);

                message = MutiLangUtil.paramAddSuccess("平台配置["+platformConfig.getPfConfig()+"]");
            } catch (Exception e) {
                e.printStackTrace();

                message = "保存平台配置["+platformConfig.getPfConfig() +"]失败";
            }

            systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
        }
        j.setMsg(message);
        return j;
    }



}
